package com.j.thetabk.thetas;

import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;

/**
 * Created by j on 2016/4/13.
 */
public class ThetaSConnectorB extends ThetaSConnectorA {

    public String fingerprint;
    public String sessionId;
    public double batteryLevel;
    public boolean storageChanged;
    public String _captureStatus;
    public long _recordedTime;
    public long _recordableTime;
    public String _latestFileUri;
    public String _batteryState;

    public String [] _cameraError=null;

    public ThetaSConnectorB(String cameraIpAddress) {
        super(cameraIpAddress);
    }

    protected void ParseState(JSONObject response) throws JSONException {

        fingerprint=response.getString("fingerprint");
        JSONObject jbtemp=response.getJSONObject("state");
        sessionId=jbtemp.getString("sessionId");
        batteryLevel=jbtemp.getDouble("batteryLevel");
        storageChanged=jbtemp.getBoolean("storageChanged");
        _captureStatus=jbtemp.getString("_captureStatus");
        _recordedTime=jbtemp.getLong("_recordedTime");
        _recordableTime=jbtemp.getLong("_recordableTime");
        _latestFileUri=jbtemp.getString("_latestFileUri");
        _batteryState=jbtemp.getString("_batteryState");

        if(jbtemp.has("_cameraError"))
        {
            JSONArray jatemp = jbtemp.getJSONArray("_cameraError");
            _cameraError=new String[jatemp.length()];
            for(int i=0;i<jatemp.length();i++)
                _cameraError[i]=jatemp.getString(i);
        }
        else
        {
            _cameraError=null;
        }
    }

    public int BatteryState()
    {
        if(_batteryState==null)
            return 0xff0000ff;
        if(_batteryState.equals("disconnect"))
            return 0xff0000ff;
        if(_batteryState.equals("charging"))
            return 0xffff0000;
        return 0xff00ff00;
    }

    public String BatteryStr()
    {
        return (int) (batteryLevel*100)+"%";
    }

    public boolean Connect() throws JSONException, IOException {

        JSONObject input= StartSessionParam();
        JSONObject output = TransmitA(3, input);
        if (IsStateDone(output)) {
            sessionId = GetSessionId(output);
            return true;
        }
        return false;
    }

    public boolean Disconnect() throws JSONException, IOException {
        JSONObject input=CloseSessionParam(sessionId);
        JSONObject output = TransmitA(3,input);
        return IsStateDone(output);
    }

    public void GetState() throws IOException, JSONException {
        JSONObject output = TransmitA(1, null);
        ParseState(output);
        Log.v("GetState", output.toString());
    }

    public boolean GetStateIdle() throws IOException, JSONException {
        JSONObject output = TransmitA(1, null);
        _captureStatus=output.getJSONObject("state").getString("_captureStatus");
        return IsIdle();
    }

    public boolean StartVideo() throws IOException, JSONException {
        JSONObject input=CaptureVideoParam(sessionId);
        JSONObject output = TransmitA(3, input);
        if (IsStateDone(output))
            return true;
        return false;
    }

    public boolean StopVideo() throws IOException, JSONException {
        JSONObject input=StopVideoParam(sessionId);
        JSONObject output = TransmitA(3, input);
        if (IsStateDone(output))
            return true;
        return false;
    }

    public InputStream GetLivePreview() throws IOException, JSONException {
        JSONObject input = LivePreviewParam(sessionId);
        return Transmit(3,input);
    }

    public boolean IsIdle()
    {
        return _captureStatus.equals("idle");
    }
}
